package demo;

import javax.sound.midi.SoundbankResource;
import java.util.Arrays;
import java.util.Scanner;


public class Demo {
    static int start; //起点
    static int end;  //终点
    static char[] vertex;
    static final int N = 65535;//表示不可连接
    static int[][] matrix = {{N,13,N,N,N,26,N},
                            {13,N,18,33,N,N,N},
                            {N,18,N,8,N,N,N},
                            {N,33,8,N,N,N,N},
                            {N,N,N,N,N,13,24},
                            {26,N,N,N,13,N,12},
                            {N,N,N,N,24,12,N}};
    static String[] enter = {"入口","狮子园","猴山","老虎洞","鸟林","天鹅湖","熊猫基地"};

    public static void calculate() {
        enter();
        char[] vertex = {'A','B','C','D','E','F','G'};
        //邻接矩阵
//        int[][] matrix = new int[vertex.length][vertex.length];
//        final int N = 65535;//表示不可连接
//        matrix[0] = new int[]{N,13,N,N,N,26,N};
//        matrix[1] = new int[]{13,N,18,33,N,N,N};
//        matrix[2] = new int[]{N,18,N,N,N,N,N};
//        matrix[3] = new int[]{N,33,8,N,N,N,N};
//        matrix[4] = new int[]{N,N,N,N,N,13,N};
//        matrix[5] = new int[]{26,N,N,N,13,N,12};
//        matrix[6] = new int[]{N,N,N,N,N,12,N};
        //创建Graph对象
        Graph graph = new Graph(vertex,matrix);
        //测试显示图的邻接矩阵
//        graph.showGraph();
        //测试dij算法
        graph.dsj(start);
//        graph.dsj(6);
        graph.showDij();
    }

    public static void updateEdge(int v1, int v2, int weight){
        matrix[v1][v2] = weight;
        matrix[v2][v1] = weight;//无向图
        System.out.println("更新成功");
    }


//    public static void main(String[] args) {
//        enter();
//        char[] vertex = {'A','B','C','D','E','F','G'};
//        //邻接矩阵
//        int[][] matrix = new int[vertex.length][vertex.length];
//        final int N = 65535;//表示不可连接
//        matrix[0] = new int[]{N,13,N,N,N,26,N};
//        matrix[1] = new int[]{13,N,18,33,N,N,N};
//        matrix[2] = new int[]{N,18,N,N,N,N,N};
//        matrix[3] = new int[]{N,33,N,N,N,N,N};
//        matrix[4] = new int[]{N,N,N,N,N,13,N};
//        matrix[5] = new int[]{26,N,N,N,13,N,12};
//        matrix[6] = new int[]{N,N,N,N,N,12,N};
//        //创建Graph对象
//        Graph graph = new Graph(vertex,matrix);
//        //测试修改
//        updateEdge(0,1,12);
//        //测试显示图的邻接矩阵
//        graph.showGraph();
//        //测试dij算法
////        graph.dsj(start);
////        graph.dsj(6);
////        graph.showDij();
//    }

    public static void enter(){
        System.out.println("请输入两景点间的所求路径");
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        String[] strings = string.split(" ");
        String s = strings[0];
        String e= strings[1];
        String[] enter = {"入口","狮子园","猴山","老虎洞","鸟林","天鹅湖","熊猫基地"};
        for(int i = 0; i < enter.length; i++){
            if(s.equals(enter[i])){
                start = i;
            }
            if(e.equals(enter[i])){
                end = i;
            }
        }
        String[] vertex = {"A","B","C","D","E","F","G"};
        for(int i = 0; i < vertex.length; i++){
            if(vertex[i].equals(s)){
                start = i;
//                System.out.println("start" + start);
            }
            if(vertex[i].equals(e)){
                end = i;
//                System.out.println("end" + end);
            }
        }

    }

}

class Graph{
    private  char[] vertex; //顶点数组
    private int[][] matrix; //邻接矩阵
    private VisitedVertex vv;//表示已经访问的顶点集合

    //构造器
    public Graph(char[] vertex , int[][] matrix){
        this.vertex = vertex;
        this.matrix = matrix;
    }

    //显示结果
    public void showDij(){
        vv.show();
    }

    //显示图
    public void showGraph(){
        for (int[] link : matrix){
            System.out.println(Arrays.toString(link));
        }
    }

    /**
     * dijkstra算法实现
     * @param index 表示出发顶点对应的下标
     */
    public void dsj(int index){
        vv = new VisitedVertex(vertex.length,index);
        update(index);//更新index顶点到周围顶点的距离和前驱顶点
        for(int j = 1; j < vertex.length; j++){
            index = vv.updateArr();//选择并返回新的访问顶点
            update(index);//更新index顶点到周围顶点的距离和前驱顶点
        }
    }

//    public void dsj(int index1, int index2){
//        vv = new VisitedVertex(vertex.length,index1);
//        update(index1);
//
//    }


    //更新index下标顶点到周围顶点的距离和周围顶点的前驱顶点
    private void update(int index){
        int len = 0;
        //根据遍历我们的邻接矩阵的matrix[index]行
        for (int j = 0; j < matrix[index].length; j++) {
            //len的含义：出发顶点到index的距离+ 从index到j这个顶点的距离之和
            len = vv.getDis(index) + matrix[index][j];
            //如果j这个顶点没有被访问过，并且len小于出发顶点到j顶点的距离，就需要更新
            if(!vv.in(j) && len < vv.getDis(j)){
                vv.updatePre(j,index);//更新j的前驱为index
                vv.updateDis(j,len);//更新出发顶点到j的距离
            }
        }
    }
}

//已访问的顶点集合
class VisitedVertex extends Demo{
    //记录各个顶点是否访问过 1表示访问过，0表示没有 会动态更新
    public int[] already_arr;
    //每个下标对应的值为前一个顶点下标，会动态更新
    public int[] pre_visited;
    //记录出发顶点到其他所有顶点的距离，比如G出发到其他顶点的最短距离，距离会动态更新，求的时就会存到dis
    public int[] dis;

    /**
     * 构造器
     * @param length：表示顶点个数
     * @param index：出发顶点对应的下标
     */
    public VisitedVertex(int length, int index){
        this.already_arr = new int[length];
        this.pre_visited = new int[length];
        this.dis = new int[length];
        //初始化dis数组,先全部置为65535，再将自己置为0，因为自己不会跟自己连
        Arrays.fill(dis,65535);
        this.already_arr[index] = 1;//设置出发顶点被访问过
        this.dis[index] = 0;
    }

    /**
     * 功能：判断index顶点是否被访问过
     * @param index
     * @return 如果访问过返回true，否则返回false
     */
    public boolean in(int index){
        return already_arr[index] == 1;
    }

    /**
     * 更新出发顶点到index的距离
     * @param index
     * @param len
     */
    public void updateDis(int index , int len){
        dis[index] = len;
    }

    /**
     * 更新pre这个顶点的前驱顶点
     * @param pre
     * @param index
     */
    public void updatePre(int pre , int index){
        pre_visited[pre] = index;
    }

    /**
     * 返回出发顶点到index顶点间的距离
     * @param index
     */
    public int getDis(int index){
        return dis[index];
    }

    //继续选择并返回新的访问顶点，比如这里的G顶点访问完后就是A点作为新的访问顶点（注意不是出发顶点）
    public int updateArr(){
        int min = 65535 , index = 0;
        for (int i = 0; i <already_arr.length; i++) {
            if(already_arr[i] == 0 && dis[i] < min){
                min = dis[i];
                index = i;
            }
        }
        //更新index顶点被访问过
        already_arr[index] = 1;
        return index;
    }

    //显示最后的结果
    public void show(){
//        System.out.println("**********************************************");
//        //输出already_arr
//        for(int i : already_arr){
//            System.out.print(i + " ");
//        }
//        System.out.println();
//        //输出pre_visited
//        for(int i : pre_visited){
//            System.out.print(i + " ");
//        }
//        System.out.println();
//        //输出dis
//        for (int i : dis){
//            System.out.print(i + " ");
//        }
//        System.out.println();

        //好看点输出最短距离
        String[] vertex = {"A","B","C","D","E","F","G"};
        int count = 0;
        for(int i : dis){
            if(i != 65535){
                if(count == end){
                    System.out.println("景点" + enter[start] +"到景点" + enter[end] + "的最短距离为" + i);
                }
//                System.out.print(vertex[count] + "(" + i + ")");
            }else{
                System.out.println("景点" + enter[start] +"到景点" + enter[end] + "无最短距离");
//                System.out.print("N");
            }
            count++;
        }
        System.out.println();
    }
}